package day1105.first;

import day1105.common.MyUtil;
import day1105.common.Student;

import java.util.List;

import static day1105.common.MyUtil.printLine;

/**
 * @author Mr.ma
 * @since 2022/11/5 17:07
 * reduce(): 规约操作，将流中元素按指定方式（加减乘除等）归纳成一个元素
 * 该方法存在方法重载:
 *      T reduce(T identity, BinaryOperator<T> accumulator);
 *      T reduce(T 恒等值（最终结果）, 二元运算接口（函数式接口）);
 *
 *      Optional<T> reduce(BinaryOperator<T> accumulator);
 *      Optional<T> reduce(二元运算接口（函数式接口）);
 *      改形式下的返回值为传入的第一个参数
 *
 *      <U> U reduce(U identity,
 *                  BiFunction<U, ? super T, U> accumulator,
 *                  BinaryOperator<U> combiner);
 **/
public class ReduceTest {
    public static void main(String[] args) {
        List<Student> studentList = MyUtil.createStudentList();
        studentList.stream().forEach(System.out::println);
        printLine("学生分数总和");
        Double aDouble = studentList.stream().map(Student::getScore).reduce((x1, x2) -> x1 += x2).get();
        System.out.println("aDouble = " + aDouble);
        Double reduce = studentList.stream().map(Student::getScore).reduce(0.0, Double::sum);
        System.out.println("reduce = " + reduce);
    }
}
